<?php

class Bids_model extends CI_Model {

    function __construct() {
        parent::__construct();
        $this->tableName = 'mis_bids';
        $this->load->model("functions_model");
        $this->load->model("consolidate_model");
    }

    function delete($tid) {
        $data['tid'] = $tid;
        $this->db->where($data);
        $this->db->delete($this->tableName);
    }

    function newBid($added_by) {
        $data = $_POST;
        $data['added_by'] = $added_by;
        $this->db->insert($this->tableName, $data);
    }

    function getOne($tid) {
        $data['tid'] = $tid;
        $this->db->select('bid_no,type,company,where_advertised,added_by,date_advertised,description,cost,date_added');
        $this->db->where($data);
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $query = $this->db->get();
        $result = array();
        foreach ($query->result_array() as $row) {
            $row['date_advertised'] = $this->functions_model->prettyDate($row['date_advertised']);
            $row['date_added'] = $this->functions_model->prettyDateTime($row['date_added']);
            $list = $this->consolidate_model->getEIOTypes($row['type']);
            if (count($list) > 0) {
                $row['type'] = $list[0]->name;
            }
            $result[] = $row;
        }
        return $result;
    }

    //    function getAll($username) {
    //       // $data['added_by'] = $username;
    //        $this->db->select('tid,bid_no,company,where_advertised,date_advertised,description,cost,date_added');
    //        //$this->db->where($data);
    //        $this->db->from($this->tableName);
    //        $this->db->order_by("tid", "desc");
    //        $query = $this->db->get();
    //        $result = array();
    //        foreach ($query->result_array() as $row) {
    //            $row['date_advertised'] = $this->functions_model->prettyDate($row['date_advertised']);
    //            $row['date_added'] = $this->functions_model->prettyDateTime($row['date_added']);
    //            $row['more'] = "<a href='javascript:loadUrl(\"#bids_result\",\"" . base_url() . "index.php?/more_info/bidsMoreInfo/" . $row['tid'] . "\")'>More details</a>";
    //
    //            unset($row['tid']);
    //            $result[] = $row;
    //        }
    //        return $result;
    //    }

    function adminGetAll($field=NULL, $search=NULL) {
        if ($field and $search) {
            if ($field == 'all') {
                $this->db->like("bid_no", $search);
                $this->db->or_like("company", $search);
                $this->db->or_like("where_advertised", $search);
                $this->db->or_like("description", $search);
                $this->db->or_like("added_by", $search);
            } else {
                $data[$field] = $search;
                $this->db->like($data);
            }
        }
        $this->db->select('date_added, tid, bid_no,company,where_advertised,date_advertised,description,cost');
        $this->db->from($this->tableName);
        $this->db->order_by("tid", "desc");
        $query = $this->db->get();
        $result = array();
        foreach ($query->result_array() as $row) {
            $tid = $row['tid'];
            //$row['added_by'] = "<a href='javascript:loadUrl(\"#bid_result\",\"" . base_url() . "index.php?/admin_mis/applicantProfile/$added_by\")'>" . $row['added_by'] . "</a>";
            $row['date_advertised'] = $this->functions_model->prettyDate($row['date_advertised']);
            $row['date_added'] = $this->functions_model->prettyDate($row['date_added']);

            $row['more'] = "<a href='javascript:loadUrl(\"#bids_result\",\"" . base_url() . "index.php?/more_info/bidsMoreInfo/" . $row['tid'] . "\")'>More details</>  ";

            if ($this->session->userdata("admin") != FALSE) {
                $row['more'].=" <a href='javascript:gotoUrl(\"" . base_url() . "index.php?/admin_mis/deleteBid/$tid\",\"Are you sure to delete this record?\")'>Delete</a> ";
            }
            unset($row['tid']);
            $result[] = $row;
        }
        return $result;
    }

}

?>
